package com.tianbing.adlottery.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.User;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditProfileController.class.getName());

	private IUserDao userDao;

	private SkinUtils skinUtils;

	// IPartyService partyService;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		User user = userDao.findUserBySysId(auth.getUserSysId());
		fb.setUser(user);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession authorization = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		User user = form.getUser();

		if (form.getPassword() != null && !form.getPassword().trim().equals("") && !isDemoUser(authorization)) {
			// refer to PasswordEditValidator,here will be executed only passed
			// PasswordEditValidator
			user.setPassword(form.getNewPassword());
		}
		user = userDao.saveUser(user);

		skinUtils.setUserAuthorization(request, response, user.getUserId(), user.getPassword(), true);
		// partyService.savePartyDetail(ModelRequest.newRequest().setParty(oldPerson));
		mv.getModel().put("tips", "Your password has been changed successfully. " + "form.getNewConfirmPassword()");
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		// List user2Categories =
		// userDao.getListUser2CategoryBy(authorization.getUserSysId(),
		// User2Category.RelationType.favorite, User2Category.Status.approve,
		// -1L,null, -1, -1, null);
		// // .getUserGroupsBy(authorization.getUserSysId(),
		// // User2Category.RelationType.favorite);
		// List categoryTrails = new ArrayList(0);
		// for (int i = 0; user2Categories != null && i <
		// user2Categories.size(); i++) {
		// User2Category pg = (User2Category) user2Categories.get(i);
		// List categoryTrail =
		// categoryDao.getListCategoryPrimaryTrailBy(pg.getCategory().getSysId());
		// categoryTrails.add(categoryTrail);
		// }
		// if (categoryTrails.size() > 0) {
		// model.put("categoryTrails", categoryTrails);
		// }
		//
		// String ptgid = ServletRequestUtils
		// .getStringParameter(request, "ptgid",
		// Global.CategoryID.place.getCategoryId());
		// List categoryTrail =
		// categoryDao.getListCategoryPrimaryTrailBy(ptgid);
		// List partySubGroups = categoryDao.getListChildCategoryBy(ptgid);
		// model.put("categoryTrail", categoryTrail);
		// model.put("partySubGroups", partySubGroups);
		return model;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	private boolean isDemoUser(UserSession authorization) {
		if (authorization != null && authorization.getUserId() != null
				&& authorization.getUserId().equals("demo@500hao.com"))
			return true;
		return false;
	}
}
